<?php
	class DashboardController extends AppController {
		var $name = 'Dashboard';
		var $uses = null; 
						
		function index(){
			$this->set('title_for_layout','Dashboard | Dorime');
			//get the user's email from session .
			$email = $this->Auth->user('email');
			$this->loadModel('User');
			// select the user's info with email,we need the user's id to select all the blog that user following.
			$user = $this->User->find('first',array(
				'conditions'=>array('User.email'=>$email),
				'fields'=>array('User.id','User.username' ,'User.pic','User.following_count','User.favorite_count'),
			));
			// find the blog that current user follow.
			$this->loadModel('UsersBlog');
			$blogID = $this->UsersBlog->find('all',array(
				'conditions'=>array('UsersBlog.users_id' => $user['User']['id'],'UsersBlog.status'=>'follow'),
				'fields'=>array('UsersBlog.id' ,'UsersBlog.blogs_id'),
			));
			// find the posts that  published by the blogs that user follow.
			$this->loadModel('Post');
			$blogIDs = array();
			for( $i = 0 ;$i < count($blogID) ; ++$i){
				$blogIDs[$i] = $blogID[$i]['UsersBlog']['blogs_id'] ;
			}
			// select the posts that  published by the blogs that user follow,first we slect the 
			$posts = $this->Post->find('all',array(
				'conditions'=>array('Post.blogs_id'=>$blogIDs),
				'fields'=>array('Post.id' ,'Post.title','Post.excerpt','Post.reblog_posts_id', 'Post.blogs_id','Post.users_id','Post.response_count'),
				'order'=>'Post.post_time desc ',
				'limit'=> 25
			));
			// select tas of every post.
			$this->loadModel('PostHasTag') ;
			$this->loadModel('Response') ;
			$this->PostHasTag->unBindModel(array('belongsTo'=>array('Post')));
			foreach($posts as $key => $value ){
				// find the author'name of the post .that  is the userame of the user_id
				$tmp = $this->User->find('first',array(
					'conditions'=>array('User.id'=>$value['Post']['users_id']),
					'fields'=>array('User.username')
				));
				$posts[$key]['Post']['username'] = $tmp['User']['username'] ;
		 		// set the blog 's icon .
		 		$posts[$key]['Post']['blogs_icon'] = '' ;
		 		//find the post's tags.
			 	$tags = $this->PostHasTag->find('all',array(
			 		'conditions'=>array('PostHasTag.posts_id'=>$value['Post']['id']),
			 		'fields'=>array('Tag.id' ,'Tag.name')
			 	));
			 	$i = 0 ;
			 	$posts[$key]['Post']['tags'][$i]['name'] = null  ;
			 	$posts[$key]['Post']['tags'][$i]['id']	= null ;
			 	foreach ($tags as $key1 => $value1 ){
			 		$posts[$key]['Post']['tags'][$i]['name'] = $value1['Tag']['name'] ;
			 		$posts[$key]['Post']['tags'][$i]['id']	 = $value1['Tag']['id'] ;
			 		++$i ;
			 	}
				// find whether the current user like the post 
		 		$responseType = $this->Response->find('first',array(
		 			'conditions'=>array('Response.users_id'=>$user['User']['id'],'Response.posts_id'=>$posts[$key]['Post']['id']),
		 			'fields'=>array('Response.type','Response.date')
		 		));
		 	 
		 		if($responseType['Response']['type'] == 'liked' ){
		 			$posts[$key]['Post']['like'] = 1 ;
		 		}else 
		 			$posts[$key]['Post']['like'] = 0 ;
		 		if($responseType['Response']['type'] == 'reblog'){
		 			$posts[$key]['Post']['reblog'] = 1 ;
		 		}else {
		 			$posts[$key]['Post']['reblog'] = 0 ;
		 		}
		 	}
			//select the tag which user has.
			$this->loadModel('UserTrackTag');
			$tmp = $this->UserTrackTag->find('all',array(
				'conditions'=>array('users_id' => $user['User']['id']) ,
				'fields'=>array('Tag.id','Tag.name')
			));
			$usertags = array();
			$i = 0 ;
	 		foreach( $tmp as $key => $value ){
	 			$usertags[$i]['name'] = $value['Tag']['name'] ;
	 			$usertags[$i]['id'] = $value['Tag']['id']  ;
	 			++$i ;
	 		}
	 		// set all the data to the view .
	 		$user = $user['User'] ;
	 		$this->set('user',$user) ;
	 		$this->set('posts',$posts);
	 		$this->set('usertags',$usertags);
		}
		
	}
?>